多线程方式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace 异步刷新进度条1
{
public partial class Form1 : Form
{
Thread[] workThreads = new Thread[3];
delegate void AsynUpdateUI(int step);
public Form1()
{
InitializeComponent();
// 将lable1'lblWriteStatus'背景色设置为透明
lblWriteStatus.BackColor = Color.Transparent;
}
private void btnWrite_Click(object sender, EventArgs e)
{
int taskCount = 10000; //任务量为10000
//for (int i = 0; i < 3; i )
//{
// if (workThreads[i] == null)
// {
this.pgbWrite.Maximum = taskCount;
this.pgbWrite.Value = 0;
DataWrite dataWrite = new DataWrite();//实例化一个写入数据的类
dataWrite.UpdateUIDelegate = UpdataUIStatus;//注册绑定更新任务状态的方法到委托
dataWrite.TaskCallBack = Accomplish;//注册绑定完成任务要调用的方法到委托
int i = this.pgbWrite.Value;
// //如果线程不存在,则创建
workThreads[i] = new Thread(new ParameterizedThreadStart(dataWrite.Write));
workThreads[i].Name = "Machine " i.ToString();
workThreads[i].IsBackground = true;
workThreads[i].Start(taskCount);
// }
// else
// {
// //已经存在,如果没有运行,则启动
// if (workThreads[i].ThreadState == ThreadState.Aborted || workThreads[i].ThreadState == ThreadState.Stopped)
// {
// this.pgbWrite.Maximum = taskCount;
// this.pgbWrite.Value = 0;
// DataWrite dataWrite = new DataWrite();//实例化一个写入数据的类
// dataWrite.UpdateUIDelegate = UpdataUIStatus;//绑定更新任务状态的委托
// dataWrite.TaskCallBack = Accomplish;//绑定完成任务要调用的委托
// workThreads[i] = new Thread(new ParameterizedThreadStart(dataWrite.Write));
// workThreads[i].Name = "Machine " i.ToString();
// workThreads[i].IsBackground = true;
// workThreads[i].Start(taskCount);
// }
// else
// {
// workThreads[i].Start(taskCount);
// }
// }
//}
}
//更新UI
private void UpdataUIStatus(int step)
{
if (InvokeRequired)
{
this.Invoke(new AsynUpdateUI(delegate(int s)
{
RefreshBar(s);
}), step);
}
else
{
RefreshBar(step);
}
}
//更新进度条
public void RefreshBar(int s)
{
this.pgbWrite.Value = s;
this.lblWriteStatus.Text = this.pgbWrite.Value.ToString() "/" this.pgbWrite.Maximum.ToString();
//string strText = this.pgbWrite.Value.ToString() "%";
//Font font = new Font("微软雅黑", (float)10, FontStyle.Regular);
//PointF pointF = new PointF(this.pgbWrite.Width / 2 - 10, this.pgbWrite.Height / 2 - 10);
//this.pgbWrite.CreateGraphics().DrawString(strText, font, Brushes.Black, pointF);
}
//完成任务时需要调用
private void Accomplish()
{
//还可以进行其他的一些完任务完成之后的逻辑处理
MessageBox.Show("任务完成");
}
private void button1_Click(object sender, EventArgs e)
{
DateTime ts = DateTime.Now;
string where = Application.StartupPath.ToString() "\\ProbingLog";
string LogName = ts.Year.ToString() ts.Month.ToString() ts.Day.ToString() ts.Hour.ToString() ts.Minute.ToString() ts.Second.ToString() ".log";
File.AppendAllText(where "\\" LogName, "ID" " " "MachineNumber" " " "IpAddress" " " "State" " " "E-mail" " " "ProbingTime" " " "\r\n", Encoding.Default);
SaveProbingLog("sb");
MessageBox.Show(string.Format(where));
//ShowSaveFileDialog();
}
static void SaveProbingLog(string args)
{
try
{
DateTime ts = DateTime.Now;
string where = Application.StartupPath.ToString() "\\ProbingLog";
string LogName = ts.Year.ToString() ts.Month.ToString() ts.Day.ToString() ts.Hour.ToString() ts.Minute.ToString() ts.Second.ToString() ".log";
FileStream aFile = new FileStream(where @"\123\" LogName, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
sw.WriteLine("为今后我们之间的进一步合作,");
sw.WriteLine("为我们之间日益增进的友谊,");
sw.Write("为朋友们的健康幸福,");
sw.Write("干杯!朋友!");
sw.Close();
}
catch (IOException ex)
{
return;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
#region 保存对话框
private void ShowSaveFileDialog()
{
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "探测日志文件(*.log)|*.log";
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//点了保存按钮进入
if (sfd.ShowDialog() == DialogResult.OK)
{
//string localFilePath = sfd.FileName.ToString(); //获得文件路径
string localFilePath = Application.StartupPath.ToString(); //获得文件路径
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") 1); //获取文件名,不带路径
//获取文件路径,不带文件名
//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
//给文件名前加上时间
//newFileName = DateTime.Now.ToString("yyyyMMdd") fileNameExt;
//在文件名里加字符
//saveFileDialog1.FileName.Insert(1,"dameng");
//System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件
////fs输出带文字或图片的文件,就看需求了
}
}
#endregion
}
}
评论